package com.fenghongzhang.day016;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class Main3Activity extends AppCompatActivity {
    private ListView lv;
    private SeekBar seekbar;
    private Button pasue;
    private Button restart;
    private Button stop;
    private Button pre;
    private Button next;
    private Button modle;

    private MyService myService;
    private MyService.Mybinder mybinder;
    private Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        lv = (ListView) findViewById(R.id.lv);
        seekbar = (SeekBar) findViewById(R.id.seekbar);
        pasue = (Button) findViewById(R.id.pasue);
        restart = (Button) findViewById(R.id.restart);
        stop = (Button) findViewById(R.id.stop);
        pre = (Button) findViewById(R.id.pre);
        next = (Button) findViewById(R.id.next);
        modle = (Button) findViewById(R.id.modle);


        //上一首
        pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myService.pre();
            }
        });
        //1,start 2,bind
        //可以在后台一致活着,知道stop
        Intent intent = new Intent(this, MyService.class);
        startService(intent);

        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                 mybinder = (MyService.Mybinder) service;
                myService = mybinder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        //绑定开启,可以调用服务中的方法.
        bindService(intent,connection,BIND_AUTO_CREATE);

        //lv的点击事件
        /**
         * 1,开始播放.
         * 2,用定时器,让进度值动起来.
         * 一个最大值,一个进度值
         */
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                myService.play(position);

                if(timer == null){
                    timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            //要最大值
                            //还要当前的进度值
                            seekbar.setMax(myService.getMax());
                            seekbar.setProgress(myService.getCurrent());
                        }
                    },0,1000);
                }
            }
        });


        //数据源
        List<Music> music = MusicUtil.getMusic(this);
        //适配器
        final MyAdapter myAdapter = new MyAdapter(music, this);
        lv.setAdapter(myAdapter);

        pasue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myService.puase();
            }
        });
        restart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myService.restart();
            }
        });

        //进度提的拖动
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            /**
             *
             * @param seekBar
             * @param progress 进度
             * @param fromUser 是否是用户拖动,不要和定时器的冲突,否则卡
             */
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if(fromUser){
                    //发给service
                    myService.seek(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        modle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final PopupWindow popupWindow = new PopupWindow();
                View inflate = LayoutInflater.from(Main3Activity.this).inflate(R.layout.pop_layout, null);
                popupWindow.setContentView(inflate);
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                TextView order = inflate.findViewById(R.id.pop_order);
                order.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Main3Activity.this, "顺序播放", Toast.LENGTH_SHORT).show();
                        myService.setModle(MusicUtil.ORDER);
                        popupWindow.dismiss();
                    }
                });


                TextView random = inflate.findViewById(R.id.pop_random);
                random.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Main3Activity.this, "随机播放", Toast.LENGTH_SHORT).show();
                        myService.setModle(MusicUtil.RANDOM);
                        popupWindow.dismiss();
                    }
                });

                TextView singe = inflate.findViewById(R.id.pop_singe);
                singe.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Main3Activity.this, "单曲播放", Toast.LENGTH_SHORT).show();
                        myService.setModle(MusicUtil.SINGE);

                        popupWindow.dismiss();
                    }
                });


                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(modle,0,-300);
            }
        });
    }
}
